/**
 * 
 */
package com.scpii.universal.hibernate.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.ViewAttributeDto;
import com.scpii.universal.hibernate.dao.ViewContentAttributeDao;
import com.scpii.universal.hibernate.entity.ViewContentAttributeTab;

/**
 * universalWeb
 * @author liuhj
 * 2012-7-24下午05:36:01
 */
@Repository
public class ViewContentAttributeDaoImpl extends BaseDaoImpl<ViewContentAttributeTab, Integer>  implements ViewContentAttributeDao {

	@Override
	public void deleteByViewId(int viewId) {
		String hql = "update ViewContentAttributeTab v " +
				"set v.intDeleteSign=?,v.datUpdateDatetime=? where v.intViewId=?";
		getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql).setParameter(0,
						GenericConstants.DELETE_SIGN_DELETED).setParameter(
						1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.format(new Date())).setParameter(2, viewId)
				.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ViewAttributeDto> getViewContentAttributes(int viewId) {
		StringBuffer hql = new StringBuffer();
		hql.append("select new com.scpii.universal.dto.ViewAttributeDto(");
		hql.append("v.intViewAttriId,v.intAttriId,v.intAttriOrder,a.varAttriName)");
		hql.append(" from ViewContentAttributeTab v,AttributeTab a");
		hql.append(" where v.intAttriId=a.intAttriId");
		hql.append(" and v.intDeleteSign=? and v.intViewId=?");
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql.toString())
				.setParameter(0, GenericConstants.DELETE_SIGN_EXIST)
				.setParameter(1, viewId).list();
	}

	@Override
	public void deleteByAttrId(int attrId) {
		String hql = "update ViewContentAttributeTab v "
				+ "set v.intDeleteSign=?,v.datUpdateDatetime=? where v.intAttriId=?";
		getHibernateTemplate()
				.getSessionFactory()
				.getCurrentSession()
				.createQuery(hql)
				.setParameter(0, GenericConstants.DELETE_SIGN_DELETED)
				.setParameter(1,
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.format(new Date())).setParameter(2, attrId)
				.executeUpdate();
	}
	
}
